package com.itic.appbase.applications.sys.sysexception.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.applications.sys.sysexception.persistence.model.SysException;
import com.itic.appbase.applications.sys.sysexception.persistence.model.SysExceptionExample;
import com.itic.appbase.applications.sys.sysexception.persistence.model.SysExceptionExample.Criteria;
import com.itic.appbase.applications.sys.sysexception.service.SysExceptionService;


@Controller
@RequestMapping(value="/sys/sysexception")
public class SysExceptionController
     extends BaseController {
    @Autowired
    private SysExceptionService sysExceptionService;


    /**
     * 跳转到主页面
     * 
     * @return String
     */
    @RequestMapping("/init")
    public String init() {
        return "sys/sysexception/sysexceptionMain";
    }


    /**
     * 跳转到主页面
     * 
     * @return String
     */
    @RequestMapping("/initView")
    public String initView(String id,Model model) {
    	if(StringHelper.isNotBlank(id)){
    		SysException sysException  = sysExceptionService.get(id);
    		model.addAttribute(sysException);
    	}else {
            throw new ParameterErrorException();
        }
        return "sys/sysexception/sysexceptionView";
    }
    

    /**
     * 查询日志列表
     * 
     * @param request
     * @param sysLog
     * @return Map
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> getLogList(HttpServletRequest request, SysException SysException) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String startTime = this.getCleanParams(request, "startTime");
        String endTime = this.getCleanParams(request, "endTime");

        // 封装查询条件
        SysExceptionExample example = new SysExceptionExample();
        example = this.handlePageQuery(request, example);
        example.setOrderByClause("CREATE_TIME DESC");

        Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
        if (null != SysException) {
            if( StringHelper.isNotBlank(SysException.getExceptionNote())){
            	criteria.andExceptionNoteLike(SysException.getExceptionNote());
            }
            //时间查询
            if (StringHelper.isNotBlank(startTime)) {
                criteria.andCreateTimeGreaterThanOrEqualTo(this.fixStartTime(startTime));
            }
            if (StringHelper.isNotBlank(endTime)) {
                criteria.andCreateTimeLessThanOrEqualTo(this.fixEndTime(endTime));
            }
        }

        Integer total = sysExceptionService.count(example);
        List<SysException> list = sysExceptionService.list(example);
       
        resultMap.put(IticConst.TOTAL_RECORDS, total); // 总数，显示总数量
        resultMap.put(IticConst.DISPLAY_RECORDS, total); // 显示display中的总数量
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode()); // 返回结果
        resultMap.put(IticConst.DATA, list); // 返回值
        return resultMap;
    }

    /**
     * 删除日志
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, String> delete(String ids) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();

        if (StringHelper.isNotBlank(ids)) {
            SysExceptionExample example = new SysExceptionExample();
            List<String> logIdList = Arrays.asList(ids.split(","));
            example.createCriteria().andIdIn(logIdList);
            SysException sysLog = new SysException();
            sysLog.setUpdateTime(new Date());
            sysLog.setIsEffective(DBConst.FALSE);
            result = sysExceptionService.deleteByExample(sysLog, example);
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }

        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
    
}